//
//  BNBaseWebView.m
//  Banknote
//
//  Created by tanxiaokang on 2017/11/15.
//  Copyright © 2017年 runnongjinfu. All rights reserved.
//

#import "BNBaseWebView.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface BNBaseWebView () <UIWebViewDelegate>

@property(nonatomic, strong)UIWebView *webView;

@end

@implementation BNBaseWebView

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];

//    [self configUI];
}

- (void)configUI {
    
    UIButton *makeMoneyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [makeMoneyButton setTitle:@"我要赚钱" forState:UIControlStateNormal];
    [makeMoneyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [makeMoneyButton addTarget:self action:@selector(makeMoneyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *makeMoneyItem = [[UIBarButtonItem alloc] initWithCustomView:makeMoneyButton];
    
    self.navigationItem.rightBarButtonItem = makeMoneyItem;
}

- (void)makeMoneyBtnClick{
    //跳转H5
}

- (void)setUrl:(NSString *)url {
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
      
    NSString * requestString = request.URL.absoluteString;
    NSLog(@"%@",requestString);
    NSLog(@"%@",@(navigationType));

    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
//    NSLog(@"%@",webView);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    NSLog(@"%@",webView.request);
    
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@".banks-list"] = ^(){
        NSArray *params = [JSContext currentArguments];
        
        NSLog(@"%@",params);
    };
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"%@",webView);
    
}

#pragma mark - Properties
- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:screenSize];
        _webView.delegate = self;
    }
    return _webView;
}

- (void)dealloc {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

@end
